package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.SyncSignedOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.SyncSignedOrderBizParamVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.SyncSignedOrderCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

@TestSuite
public class Ts_SyncSignedOrder {

	private static SyncSignedOrderReq syncSignedOrderReq;
	private static SyncSignedOrderCall syncSignedOrderCall;

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单的运单全部签收，同步签收状态");
	}

	@Before
	public void caseUp() {
		syncSignedOrderReq = new SyncSignedOrderReq();
		syncSignedOrderCall = new SyncSignedOrderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syncSignedOrderCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncSignedOrder_001() {
		Logger.start(true, "拼邮订单的运单国际段签收，订单签收状态为false");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);
			TradingSyncService.getSyncResult(orderId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			String billCode = TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			// 国际段签收
			TradingCallService.saveLogisticsInfo(billCode);

			SyncSignedOrderBizParamVo bizParam = new SyncSignedOrderBizParamVo();

			bizParam.setOrderId(orderId);
			bizParam.setBillCode(billCode);
			bizParam.setNodeStatus(5);

			syncSignedOrderReq.setAppId("autotest");
			syncSignedOrderReq.setBizkey(String.valueOf(orderId));
			syncSignedOrderReq.setBizParam(bizParam);
			syncSignedOrderReq.setBizType("LogisticsNotify");

			syncSignedOrderCall.setData(syncSignedOrderReq);
			syncSignedOrderCall.callService();

			Logger.verifyEquals("ok", syncSignedOrderCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.verifyEquals(false, actualOrder1.getLogisticSign(),
					"验证数据库Logistic");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncSignedOrder_002() {
		Logger.start(true, "拼邮订单的运单国际、国内段均签收，订单签收状态为true");
		try {

			// 前置条件准备开始，下单支付，签收等
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);
			TradingSyncService.getSyncResult(orderId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// Long orderId = 1400229841L;
			// Long sellerId = 24620496L;

			String billCode = TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			// 国际段签收
			TradingCallService.saveLogisticsInfo(billCode);

			billCode = TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 国内段签收
			TradingCallService.saveLogisticsInfo(billCode);

			SyncSignedOrderBizParamVo bizParam = new SyncSignedOrderBizParamVo();

			bizParam.setOrderId(orderId);
			bizParam.setBillCode(billCode);
			bizParam.setNodeStatus(5);

			syncSignedOrderReq.setAppId("autotest");
			syncSignedOrderReq.setBizkey(String.valueOf(orderId));
			syncSignedOrderReq.setBizParam(bizParam);
			syncSignedOrderReq.setBizType("LogisticsNotify");

			syncSignedOrderCall.setData(syncSignedOrderReq);
			syncSignedOrderCall.callService();

			Logger.verifyEquals("ok", syncSignedOrderCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.verifyEquals(true, actualOrder1.getLogisticSign(),
					"验证数据库Logistic");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncSignedOrder_003() {
		Logger.start(true, "直邮订单签收，订单签收状态为true");
		try {

			// 前置条件准备开始，下单支付，签收等
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);
			TradingSyncService.getSyncResult(orderId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			//
			// Long orderId = 1430118205L;
			// Long sellerId = 20224936L;

			String billCode = TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			TradingCallService.saveLogisticsInfo(billCode);

			SyncSignedOrderBizParamVo bizParam = new SyncSignedOrderBizParamVo();

			bizParam.setOrderId(orderId);
			bizParam.setBillCode(billCode);
			bizParam.setNodeStatus(5);

			syncSignedOrderReq.setAppId("autotest");
			syncSignedOrderReq.setBizkey(String.valueOf(orderId));
			syncSignedOrderReq.setBizParam(bizParam);
			syncSignedOrderReq.setBizType("LogisticsNotify");

			syncSignedOrderCall.setData(syncSignedOrderReq);
			syncSignedOrderCall.callService();

			Logger.verifyEquals("ok", syncSignedOrderCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.verifyEquals(true, actualOrder1.getLogisticSign(),
					"验证数据库Logistic");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static void main(String[] args) {

		TradingCallService.saveLogisticsInfo("BSQFJB1520673458");
	}
}